import type { TUserInfo } from '@/api/user/type';
import { defineStore } from 'pinia';
import { ref } from 'vue';
import avatar from '@/static/avatar.jpg';

const initState = {
  id: 0,
  phone: '',
  email: '',
  name: '未登录，请登录',
  avatar,
  accessToken: '',
  refreshToken: ''
};

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref<TUserInfo>({ ...initState });

    const setUserInfo = (val: TUserInfo) => {
      userInfo.value = val;
    };

    const clearUserInfo = () => {
      userInfo.value = { ...initState };
    };
    const reset = () => {
      userInfo.value = { ...initState };
    };
    const isLogined = computed(() => !!userInfo.value.accessToken);

    return {
      userInfo,
      setUserInfo,
      clearUserInfo,
      isLogined,
      reset
    };
  },
  {
    persist: true
  }
);
